//
//  FileManger.m
//  MBObjectServer
//
//  Created by admin on 3/6/14.
//  Copyright (c) 2014 MBpayer. All rights reserved.
//

#import "FileManger.h"

@implementation FileManger
//获取Documents目录
-(NSString *)dirDoc{
    //[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"app_home_doc: %@",documentsDirectory);
    return documentsDirectory;
}
//创建文件夹
-(BOOL)createDir:(NSString *)dirname{
    NSString *documentsPath =[self dirDoc];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    // 创建目录
    BOOL res=[fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    if (res) {
        NSLog(@"%@文件夹创建成功",dirname);
    }else
        NSLog(@"%@文件夹创建失败",dirname);
    return res;
}
//写文件
-(void)writeFile:(NSString *)filename withDir:(NSString *)dirname WithContent:(NSString *)contentdata{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    if ([self createDir:dirname]) {
        NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
        NSString *content= contentdata;
        BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        if (res) {
            NSLog(@"文件写入成功");
        }else
            NSLog(@"文件写入失败");
    }
    
}
//写文件
-(void)writeFile:(NSString *)filename withDir:(NSString *)dirname
                appendString:(NSString *)contentdata{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    
    if ([self fileisexits:filename withDir:dirname]) {
        NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
        
        NSString *content=[self readFile:filename withDir:dirname];
        if (content != nil) {
            NSMutableString * contentstr = [NSMutableString stringWithString:content];
            [contentstr appendString:[NSString stringWithFormat:@"%@+",contentdata]];
            BOOL res=[contentstr writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
            if (res) {
                NSLog(@"文件写入成功");
            }else
                NSLog(@"文件写入失败");
            
        }else{
            [content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
        }

    }else{
        if ([self createDir:dirname]) {
            NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
            
            NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
            if (content != nil) {
                NSMutableString * contentstr = [NSMutableString stringWithString:content];
                [contentstr appendString:[NSString stringWithFormat:@"%@+",contentdata]];
                BOOL res=[contentstr writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
                if (res) {
                    NSLog(@"文件写入成功");
                }else
                    NSLog(@"文件写入失败");
                
            }else{
                BOOL res=[content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
            }

        }
    
    }
}
-(BOOL)fileisexits:(NSString *)filename withDir:(NSString *)dirname{
    //创建NSFileManager实例
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
    return [fm fileExistsAtPath:testDirectory];
}
//写文件
-(void)writeFile:(NSString *)filename withDir:(NSString *)dirname WithContentdata:(NSData *)contentdata{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    if ([self createDir:dirname]) {
        NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
        BOOL res=[contentdata writeToFile:testPath atomically:YES];
        if (res) {
            NSLog(@"文件写入成功");
        }else
            NSLog(@"文件写入失败");
    }

    
}
//写文件
-(void)writeFile:(NSString *)filename withDir:(NSString *)dirname WithContentDictionary:(NSDictionary *)contentdictionary{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    if ([self createDir:dirname]) {
        NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
        BOOL res=[contentdictionary writeToFile:testPath atomically:YES];
        if (res) {
            NSLog(@"文件写入成功");
        }else
            NSLog(@"文件写入失败");
    }
    
    
}
//写文件
-(void)writeFile:(NSString *)filename withDir:(NSString *)dirname WithContentArray:(NSArray *)contentarray{
    
    
    
    
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    if ([self createDir:dirname]) {
        NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
        BOOL res=[contentarray writeToFile:testPath atomically:YES];
        if (res) {
            NSLog(@"文件写入成功");
        }else
            NSLog(@"文件写入失败");
    }
    
}

//读文件
-(NSString *)readFile:(NSString *)filename withDir:(NSString *)dirname{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
    NSData *data = [NSData dataWithContentsOfFile:testPath];
    //    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    NSString *content=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"文件读取成功: %@",content);
    return content;
}
//读文件
-(NSDictionary *)readFileDictionary:(NSString *)filename withDir:(NSString *)dirname{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:testPath];
//    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    //    NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
    //    NSLog(@"文件读取成功: %@",content);
    return dic;
}
//读文件
-(NSData *)readFileData:(NSString *)filename withDir:(NSString *)dirname{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
    NSData *data = [NSData dataWithContentsOfFile:testPath];
    //    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
    //    NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
    //    NSLog(@"文件读取成功: %@",content);
    return data;
}

//读文件
-(NSArray *)readFileArray:(NSString *)filename withDir:(NSString *)dirname{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
    NSArray *array = [NSArray arrayWithContentsOfFile:testPath];
//    NSLog(@"文件读取成功: %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
//    NSString *content=[NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"文件读取成功: %@",content);
    return array;
}
//删除文件
-(BOOL)deleteFile:(NSString *)filename withDir:(NSString *)dirname{
    NSString *documentsPath =[self dirDoc];
    NSString *testDirectory = [documentsPath stringByAppendingPathComponent:dirname];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *testPath = [testDirectory stringByAppendingPathComponent:filename];
    BOOL res=[fileManager removeItemAtPath:testPath error:nil];
    if (res) {
        NSLog(@"文件删除成功");
    }else
        NSLog(@"文件删除失败");
    NSLog(@"文件是否存在: %@",[fileManager isExecutableFileAtPath:testPath]?@"YES":@"NO");
    return res;
}

@end
